package com.example.utils;

import java.sql.*;

public class Databasetest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/shixun?serverTimezone=GMT%2B8";
        String username = "root";
        String password = "root";
        String tableName = "sort";

        try (Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM " + tableName)) {

            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 打印表头
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnName(i);
                System.out.print(columnName + "\t");
            }

            System.out.println();
            // 打印表内容
            while (resultSet.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    String value = resultSet.getString(i);
                    System.out.print(value + "\t");
                }
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
